home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9692 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.0 KB

  1. Path: io.UWinnipeg.ca!wsimpson
  2. From: Bill Simpson <wsimpson@uwinnipeg.ca>
  3. Newsgroups: comp.lang.c
  4. Subject: while loop problem
  5. Date: Tue, 12 Mar 1996 13:36:41 -0600
  6. Organization: The University of Manitoba
  7. Message-ID: <Pine.OSF.3.91.960312133449.7844B-100000@io.UWinnipeg.ca>
  8. NNTP-Posting-Host: io.uwinnipeg.ca
  9. Mime-Version: 1.0
  10. Content-Type: TEXT/PLAIN; charset=US-ASCII
  11.  
  12. Consider the following code fragment:
  13.  
  14. y=2000; z=1000;
  15. x=0;
  16. while(x<=XMAX)
  17.     {
  18.     x+=(int) erand(mean);
  19.     setdot(x,y,z);
  20.     }
  21.     
  22. It plots a line of dots that are randomly (exponentially) spaced, giving
  23. a 1D spatial Poisson process.
  24. The problem is that dot x coordinates can only be between 0 and XMAX.
  25. The above code will also attempt to plot one point at an x value >XMAX
  26. before it terminates.
  27.  
  28. Is there a nice way to write the code so it works properly?
  29.  
  30. The only way I have thought of is
  31. y=2000; z=1000;
  32. x=0;
  33. while(x<=XMAX)
  34.     {
  35.     x+=(int) erand(mean);
  36.     if(x<=XMAX)
  37.         setdot(x,y,z);
  38.     }
  39.  
  40. which seems very clumsy since the same test is done twice.
  41.  
  42. Thanks very much for any help.
  43.  
  44. Bill Simpson
  45.